//
//  WYCbdAddCardController.m
//  信用卡管理
//
//  Created by 中企互联 on 2017/11/22.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCNavAddCardController.h"
#import "MyTextField.h"
#import "WYCBankModel.h"
#import "CustomPickerView.h"
#import "WYCbankPickerView.h"
#import "WYCBankModel.h"
#import "WYCAddCreditController.h"

@interface WYCNavAddCardController ()


/** 输入框*/
@property(nonatomic,strong)MyTextField * myteXfield;


/** 输入框*/
@property(nonatomic,strong)MyTextField * cardTextFirld;


/** 选择银行*/
@property(nonatomic,strong)UITextField * sexField;

@property (nonatomic,strong) NSMutableArray *resultArray;


@property (nonatomic, strong) UIToolbar *toolBar;
@property (nonatomic,weak) CustomPickerView *provinceCustomPickerView;
@property (nonatomic,weak) CustomPickerView *CityCustomPickerView;

@property (nonatomic,weak)WYCbankPickerView  *bankPickerView;

/** 银行模型*/
@property(nonatomic,strong)WYCBankModel *bankModel;
@end

@implementation WYCNavAddCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"信用卡正面";
    
    [self setupNav];
    
    [self setupbanlist];
    
    self.resultArray = [[NSMutableArray alloc] init];
    
    
}
- (void)setupNav{
    
    //照片
    UIImageView * iphoneimage = [[UIImageView alloc] init];
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.bankCardUrl]];
    iphoneimage.image = [UIImage imageWithData:data];
    iphoneimage.contentMode = UIViewContentModeScaleAspectFill;
    iphoneimage.clipsToBounds=YES;//  是否剪切掉超出
    iphoneimage.frame = CGRectMake(40 * px, NavHeight + 40 * px, ScreenWidth - 80 * px, 600 * px);
    [self.view addSubview:iphoneimage];
    
    UILabel * showLbel = [[UILabel alloc] init];
    showLbel.text = @"请确认卡信息是否正确";
    showLbel.font = [UIFont boldSystemFontOfSize:60 * px];
    showLbel.textAlignment = NSTextAlignmentCenter;
    showLbel.textColor = [UIColor blackColor];
    showLbel.frame = CGRectMake(0, CGRectGetMaxY(iphoneimage.frame) + 80 * px, ScreenWidth, 70 * px);
    [self.view addSubview:showLbel];
    
    
    
    //姓名
    UILabel * nameLbel = [[UILabel alloc] init];
    nameLbel.text = @"银行卡号:";
    nameLbel.font = [UIFont systemFontOfSize:BigMiddleFont];
    nameLbel.textColor = WYCRGBColor(120, 112, 115);
    nameLbel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(showLbel.frame) + 80 * px, 300 * px, 60 * px);
    [self.view addSubview:nameLbel];
    
    
    //+1
    MyTextField * nameField = [[MyTextField alloc] init];
    nameField.frame = CGRectMake(CGRectGetMaxX(nameLbel.frame) + 20 * px, nameLbel.y, ScreenWidth, nameLbel.height);
    nameField.font = [UIFont systemFontOfSize:BigMiddleFont];
    nameField.text = self.cardNumber;
    
    nameField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:nameField];
    self.cardTextFirld = nameField;
    
    
    //开户银行
    UILabel * sexlabel = [[UILabel alloc] init];
    sexlabel.text = @"开户银行:";
    sexlabel.font = [UIFont systemFontOfSize:BigMiddleFont];
    sexlabel.textColor = WYCRGBColor(120, 112, 115);
    sexlabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(nameLbel.frame) + 80 * px, nameLbel.width, 60 * px);
    [self.view addSubview:sexlabel];
    
    
    //预留手机号
    UITextField * sexField = [[UITextField alloc] init];
    sexField.frame = CGRectMake(CGRectGetMaxX(sexlabel.frame) + 20 * px, sexlabel.y, ScreenWidth, sexlabel.height);
    sexField.font = [UIFont systemFontOfSize:BigMiddleFont];
    //    sexField.enabled = NO;
    sexField.placeholder = @"银行预留手机号";
    sexField.text = [NSString stringWithFormat:@"%@",self.bankName];
    //    sexField.backgroundColor = [UIColor redColor];
    sexField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:sexField];
    self.sexField = sexField;
    
    
    //开户银行
    UILabel * iphonelabel = [[UILabel alloc] init];
    iphonelabel.text = @"预留手机号:";
    iphonelabel.font = [UIFont systemFontOfSize:BigMiddleFont];
    iphonelabel.textColor = WYCRGBColor(120, 112, 115);
    iphonelabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(sexlabel.frame) + 80 * px, nameLbel.width, 60 * px);
    [self.view addSubview:iphonelabel];
    
    
    //预留手机号
    MyTextField * iphoneField = [[MyTextField alloc] init];
    iphoneField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 20 * px, iphonelabel.y, ScreenWidth, iphonelabel.height);
    iphoneField.placeholder = @"银行预留手机号";
    iphoneField.font = [UIFont systemFontOfSize:BigMiddleFont];
    iphoneField.keyboardType = UIKeyboardTypeNumberPad;
    iphoneField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:iphoneField];
    self.myteXfield = iphoneField;
    
    
    
    //登录按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(40 * px, CGRectGetMaxY(iphonelabel.frame) + 80 * px, ScreenWidth - 80 * px, 150 * px);
    loginButton.backgroundColor = WYCmainColor;
    loginButton.layer.cornerRadius = 5;
    [loginButton setTitle:@"保存" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:72 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
}
- (void)setupbanlist{
    
    [HttpTool POST:BankList params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                WYCBankModel * model = [WYCBankModel mj_objectWithKeyValues:dict];
                [self.resultArray addObject:model];
                self.bankModel = _bankModel;
                
            }
        }
        [self.bankPickerView refreshWithSourceData:self.resultArray];
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    // 市
    WYCbankPickerView *bankView = [[WYCbankPickerView alloc]initWithSourceData:self.resultArray confirmBlock:^(WYCBankModel *bankModel) {
        _bankModel = bankModel;
        [self.view endEditing:YES];
        self.sexField.text = bankModel.bankName;
        self.bankId = bankModel.bankId;
        
    } cancelBlock:^(WYCBankModel *bankModel) {
        [self.view endEditing:YES];
        self.sexField.text = @"";
    }];
    bankView.frame = CGRectMake(0, 0, ScreenWidth, 280);
    self.bankPickerView = bankView;
    self.sexField.inputView = self.bankPickerView;
}


- (void)loginClick {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documnetPath = [documents stringByAppendingPathComponent:@"dic.plist"];
    //准备要存入的字典
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.cardTextFirld.text,@"cardNumber",self.sexField.text,@"name",self.myteXfield.text,@"iphonenumeber",self.bankCardUrl,@"bankCardUrl",self.bankId,@"bankId",nil];
    //将字典存入指定的本地文件
    [dic writeToFile:documnetPath atomically:YES];
    //可对已经存储的字典操作
    NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:documnetPath];
    NSLog(@"%@", documnetPath);
    
    
    if (self.myteXfield.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入银行预留手机号"];
        return;
    }else{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"card" object:nil];
        NSArray *temArray = self.navigationController.viewControllers;
        
        for(UIViewController *temVC in temArray)
            
        {
            
            if ([temVC isKindOfClass:[WYCAddCreditController class]])
                
            {
                
                [self.navigationController popToViewController:temVC animated:YES];
                
            }
            
        }
    }
}



@end

